﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.Domain.ScRowDomain
{
    public class ScRowInfoService : IScRowInfoService
    {
        private readonly ILogUtil _LogUtil;
        private readonly ISnowflake _Snowflake;
        private readonly IScRowInfoRepository _ScRowInfoRepository;
        public ScRowInfoService(ILogUtil logUtil
            , ISnowflake snowflake
            , IScRowInfoRepository scRowInfoRepository)
        {
            _LogUtil = logUtil;
            _Snowflake = snowflake;
            _ScRowInfoRepository = scRowInfoRepository;
        }

        public async Task<bool> CreateScRowInfoAsync(ScRowInfo scRowInfo)
        {
            scRowInfo.ID = _Snowflake.GetId();
            bool isOk = await _ScRowInfoRepository.InsertAsync(scRowInfo);

            return isOk;
        }

        public async Task<bool> DeleteScRowInfoAsync(List<ScRowInfo> scRowInfoList)
        {
            try
            {
                _ScRowInfoRepository.AsTenant().BeginTran();

                foreach (var item in scRowInfoList)
                {
                    await _ScRowInfoRepository.DeleteByIdAsync(item.ID);
                }

                _ScRowInfoRepository.AsTenant().CommitTran();

                return true;
            }
            catch (Exception ex)
            {
                _ScRowInfoRepository.AsTenant().RollbackTran();
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 根据id删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteScRowInfoById(long id)
        {
            return await _ScRowInfoRepository.DeleteByIdAsync(id);
        }

        public async Task<PageDto<ScRowInfo>> GetPagedScRowInfoAsync(QueryScRowInfoDto queryScRowInfo)
        {
            var exp = Expressionable.Create<ScRowInfo>();
            //exp.AndIF(queryScRowInfo.Row.Length>0, it => it.Row == queryScRowInfo.Row);
            exp.AndIF(!string.IsNullOrEmpty(queryScRowInfo.Row), it => it.Row.Contains(queryScRowInfo.Row));
            exp.AndIF(!string.IsNullOrEmpty(queryScRowInfo.ScEquipmentCode), it => it.ScEquipmentCode.Contains(queryScRowInfo.ScEquipmentCode));

            PageDto<ScRowInfo> pagedData = new PageDto<ScRowInfo>();
            RefAsync<int> totalCount = 0;

            var result = await _ScRowInfoRepository.AsSugarClient()
                .Queryable<ScRowInfo>()
                .Where(exp.ToExpression())
                .ToPageListAsync(queryScRowInfo.PageIndex, queryScRowInfo.PageSize, totalCount);

            pagedData.Result = result;
            pagedData.Total = totalCount;

            return pagedData;
        }
        /// <summary>
        /// 根据id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ScRowInfo> GetScRowInfoByIdAsync(long id)
        {
            return await _ScRowInfoRepository.GetByIdAsync(id);
        }

        public async Task<bool> ModifyScRowInfoAsync(ScRowInfo scRowInfo)
        {
            bool isOk = await _ScRowInfoRepository.UpdateAsync(scRowInfo);

            return isOk;
        }
    }
}
